package com.sso.oa.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.sso.common.cons.Const;
import org.sso.common.cons.ParamConst;
import org.sso.common.cookie.TokenHelper;
import org.sso.common.domian.Token;
import org.sso.common.util.HttpUtil;

/**
 * 同域sso
 * 
 * @author spjich
 *
 */
public class ReqInterceptor extends HandlerInterceptorAdapter {

	private static final Logger logger = LoggerFactory.getLogger(ReqInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Token token = TokenHelper.getToken(request);
		if (token == null) {
			String retUrl = HttpUtil.getQueryString(request);
			logger.info("token为空，重定向到登录页面...|" + retUrl);
			response.sendRedirect(HttpUtil.encodeRetURL(ParamConst.ssoUrl, Const.ReturnURL, retUrl));
		}
		return true;
	}

}
